package cn.wolf.netty.inboundhandlerandoutboundhandler;


import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * 自定义处理器
 */
public class MyServerHandler extends SimpleChannelInboundHandler<Long> {


    /**
     * @param ctx the {@link ChannelHandlerContext} which this {@link SimpleChannelInboundHandler}
     *            belongs to
     * @param msg the message to handle
     * @throws Exception 异常
     */
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Long msg) throws Exception {
        System.out.println("从客户端:" + ctx.channel().remoteAddress() + ",读取到Long数据:" + msg);
        //给客户端回复消息
        ctx.writeAndFlush(98765L);
    }


    /**
     * 异常处理
     *
     * @param ctx   上下文对象
     * @param cause 异常内容
     * @throws Exception 异常
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
